#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <poll.h>

#define CLOSE_CMD _IO(0XEF, 1)     /* 关闭命令 */
#define OPEN_CMD _IO(0XEF, 2)      /* 打开命令 */
#define SET_CMD _IOW(0XEF, 1, int) /* 设置命令 */

int main(int argc, char *argv[])
{
    struct pollfd fds;

    int ret = 0;
    int fd = 0;
    char *filename;
    unsigned char data;

    if (argc != 2)
    {
        printf("Error usage \r\n");
        return -1;
    }

    /* 获取文件名称 */
    filename = argv[1];

    /* 以读写方式打开文件 */
    fd = open(filename, O_RDONLY | O_NONBLOCK); /* 以非阻塞方式打开 */
    if (fd < 0)
    {
        printf("can't open timer file\r\n");
        return -1;
    }

    /* 循环读取按键值 */
    while (1)
    {

        fds.fd = fd;
        fds.events = POLLIN;

        ret = poll(&fds, 1, 500);
        switch (ret)
        {
        case 0: /* 超时 */
            break;
        case -1: /* 错误 */
            break;
        default: /*  */
            if (fds.events | POLLIN)
            {
                ret = read(fd, &data, sizeof(data));
                if (ret < 0)
                {
                }
                else
                {
                    if (data)
                        printf("按键值 %d\r\n", data);
                }
            }
            break;
        }
    }

    /* 关闭文件 */
    close(fd);

    return fd;
}